/* Sample UDP client */

#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>


int main(int argc, char**argv)
{
    int sockfd,n;
    struct sockaddr_in servaddr,cliaddr;
    char sendline[1000];
    char recvline[1000];
    char * inic_msg = getpid();;
    
    if (argc != 2)
    {
        printf("usage:  udpcli <IP address>\n");
        exit(1);
    }
   
    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr=inet_addr(argv[1]);
    servaddr.sin_port=htons(32000);

    /* avisa ao servidor que um cliente entrou =) */

    sendto(sockfd,sendline,strlen(sendline),0, (struct sockaddr *)&servaddr,sizeof(servaddr)); 

    while (fgets(sendline, 10000,stdin) != NULL) {
        sendto(sockfd,sendline,strlen(sendline),0,
                (struct sockaddr *)&servaddr,sizeof(servaddr));
        n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);
        recvline[n]=0;
/*        fputs(recvline,stdout);*/
    }
    return EXIT_SUCCESS;
}
